package report4;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author zouran
 * createDate：2023/5/17  20:44
 */
public class Main {
    public static boolean judge(String input, int requestLength) {
        boolean b = true;
        for (char c : input.toCharArray()) {
            int a = Integer.parseInt(String.valueOf(c));
            if (a > 5 || a < 0) {
                b = false;
                System.out.println("您输入的序列有误，请重新输入0-5数字组成的无规律字符串作为页面访问序列，长度为" + requestLength);
                break;
            }
        }
        return b;
    }

    public static void exe() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入页面访问序列长度（15-25，含端点）：");
        int requestLength;
        while (true) {
            requestLength = scanner.nextInt();
            if (requestLength > 25 || requestLength < 15) {
                System.out.println("您输入的页面访问序列长度超出给定范围，请重新输入15-25（含端点）的数字：");
            } else break;
        }
        System.out.println("请输入物理块个数（3-5，含端点）：");
        int count;
        while (true) {
            count = scanner.nextInt();
            if (count > 5 || count < 3) {
                System.out.println("您输入的物理块个数超出给定范围，请重新输入3-5（含端点）的数字：");
            } else break;
        }
        List<Integer> requestList = new ArrayList<>();
        System.out.println("请选择页面访问序列生成方式：1、用户输入；2、随机生成");
        int way = scanner.nextInt();
        if (way == 1) {
            scanner.nextLine();
            System.out.println("请输入0-5数字组成的无规律字符串作为页面访问序列，长度为" + requestLength);
            StringBuilder input = new StringBuilder();
            String string;
            boolean link = false;
            while (true) {
                string = scanner.nextLine();
                if (link) {
                    input.append(string);
                } else {
                    input = new StringBuilder(string);
                }
                boolean correct = false;
                if (input.toString().toCharArray().length < requestLength) {
                    link = true;
                    System.out.printf("您已输入%d长度的字符串，还需输入%d长度的字符串\n", input.toString().toCharArray().length, requestLength - input.toString().toCharArray().length);
                    System.out.println("请继续输入0-5数字组成的无规律字符串作为页面访问序列，长度为" + (requestLength - input.toString().toCharArray().length));
                } else if (input.toString().toCharArray().length > requestLength) {
                    link = false;
                    System.out.println("您输入的字符串超长，系统将进行自动截取Y或由用户重新输入N（Y/N）");
                    String c = scanner.next();
                    if (c.equals("Y")) {
                        input = new StringBuilder(input.substring(input.toString().toCharArray().length - requestLength, input.toString().toCharArray().length));
                        if (judge(input.toString(), requestLength))
                            correct = true;
                    } else {
                        System.out.println("请输入0-5数字组成的无规律字符串作为页面访问序列，长度为" + requestLength);
                    }
                } else {
                    link = false;
                    if (judge(input.toString(), requestLength))
                        correct = true;
                }
                if (correct) break;
            }
            for (char c : input.toString().toCharArray()) {
                requestList.add(Integer.parseInt(String.valueOf(c)));
            }
        } else {
            requestList = new Pagination(requestLength, count).randomRequestList();
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (Integer integer : requestList) {
            stringBuilder.append(integer);
        }
        System.out.println("页面访问序列为：" + stringBuilder);
        System.out.println("物理块个数为：" + count);
        while (true) {
            System.out.println("请选择要执行的页面选择算法：1、OPT；2、FIFO；3、LRU；4、CLOCK：5、退出");
            int choice = scanner.nextInt();
            if (choice > 5 || choice < 1) {
                System.out.println("输入不合法");
                continue;
            }
            if (choice == 5) break;
            Pagination pagination = new Pagination(requestLength, count, new ArrayList<>(requestList));
            switch (choice) {
                case 1:
                    pagination.OPTorLRU("OPT");
                    break;
                case 2:
                    pagination.FIFO();
                    break;
                case 3:
                    pagination.OPTorLRU("LRU");
                    break;
                case 4:
                    pagination.CLOCK();
                    break;
            }
        }
    }

    public static void main(String[] a) {
        exe();
    }
}
